package com.mall.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.api.PmsProductAttributeApi;
import com.mall.db.api.PmsProductAttributeCategoryApi;
import com.mall.pojo.PmsProductAttribute;
import com.mall.pojo.PmsProductAttributeCategory;
import com.mall.vo.PpacVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PmsProductAttributeCategoryService {

    @DubboReference
    private PmsProductAttributeCategoryApi pmsProductAttributeCategoryApi;
    @DubboReference
    private PmsProductAttributeApi pmsProductAttributeApi;

    //添加商品属性分类
    public void create(String name) {
        pmsProductAttributeCategoryApi.create(name);
    }

    //删除单个商品属性分类
    public void delete(Integer id) {
        pmsProductAttributeCategoryApi.delete(id);
    }

    //分页获取所有商品属性分类
    public Page<PmsProductAttributeCategory> list(Integer pageNum, Integer pageSize) {
        return pmsProductAttributeCategoryApi.list(pageNum, pageSize);
    }

    //修改商品属性分类
    public void update(Integer id, String name) {
        pmsProductAttributeCategoryApi.update(id, name);
    }

    //获取单个商品属性分类信息
    public PmsProductAttributeCategory selectById(Integer id) {
        return pmsProductAttributeCategoryApi.selectById(id);
    }

    //获取所有商品属性分类及其下属性
    public List<PpacVo> withAttr() {
        List<PmsProductAttributeCategory> listppac = pmsProductAttributeCategoryApi.selectAll();
        //没有商品属性的话直接给一个空集合返回 避免空指针
        if (listppac == null) {
            return new ArrayList<PpacVo>();
        }

        List<PpacVo> list = listppac.stream().map(ppac -> {

            PpacVo ppacVo = new PpacVo();
            //根据一张表的id去查另一张表的属性
            List<PmsProductAttribute> ppa = pmsProductAttributeApi.selectById(ppac.getId());
            ppacVo.setPpac(ppac);
            ppacVo.setPpa(ppa);

            return ppacVo;
        }).collect(Collectors.toList());

        return list;
    }
}
